package other;

import java.io.File;
import java.util.List;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.scene.layout.*;

// 文件选择框
public class FileChooserDemo extends Application { 
	private Pane root = new Pane();
	private Button openFile;
	private Button saveFile;
	
	public static void main(String[] args) {
		Application.launch(args);
	}
	
	@Override
	public void start(Stage stage) {
		
		openFile = new Button("打开文件对话框");
		openFile.setLayoutX(30);
		openFile.setLayoutY(50);
		
		saveFile = new Button("打开文件保存对话框");
		saveFile.setLayoutX(30);
		saveFile.setLayoutY(100);
		root.getChildren().addAll(openFile, saveFile);
		
		Scene scene = new Scene(root, 300, 200);
		stage.setScene(scene);
		stage.setTitle("文件选择");
		stage.show();
		
		// 事件
		// 打开文件
		openFile.setOnMouseClicked(e -> {
			FileChooser fc = new FileChooser();
//			DirectoryChooser dc = new DirectoryChooser();	// 文件夹选择器（其余同FileChooser）
			fc.setTitle("open");	// 设置名字
			fc.setInitialDirectory(new File("D:" + File.separator + "Google浏览器下载"));	// 设置初始打开路径
			fc.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("图片类型","*.jpg","*.png"));	// 筛选后缀名
			
			File file = fc.showOpenDialog(stage);	// file可表示文件或文件夹
//			List<File> files = fc.showOpenMultipleDialog(stage);	// 此时可以多选
			if(file == null) {
				return;
			}
			System.out.println(file.getAbsolutePath());
		});
		
		// 保存文件
		saveFile.setOnMouseClicked(e -> {
			FileChooser fc = new FileChooser();
			fc.showSaveDialog(stage);
		});
	}
}





